/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.listener.handler;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.service.context.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneOfferInstService;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.GoodsMemberDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.MemberGoodDTO;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.CuccOfferMemberCache;
import com.iwhalecloud.bss.kite.cucc.service.pos.handler.CuccPosServiceUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.ztesoft.bss.common.util.ContextUtil;

/**
 * <Description>初始化成员附加商品 <br>
 *
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年11月24日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.listener.handler <br>
 */
@Service
public class LocalMemberMainOfferHandler {

    @Autowired
    private IOperateSceneOfferInstService operateSceneOfferInstService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    public void init(String sceneInstId, String parentOfferInstId, String custId) {
        if (ServiceContext.getIsBatchAccept()) {
            return;
        }
        KiteOfferInst parentOfferInst = this.querySceneInstService.getOfferInst(custId, parentOfferInstId);
        String cateId = parentOfferInst.getAttrValue("cateId");
        if(KiteStringUtils.isEmpty(cateId)){
            cateId = Optional.ofNullable(OfferCache.get(parentOfferInst.getOfferId())).map(Offer::getCateId).orElse("");
        }
        if(KiteStringUtils.isNotEmpty(cateId)&& KiteListUtils.isNotEmpty(DcPublicCache.getByPkey("20210506001",cateId))){
            return;
        }
        List<GoodsMemberDTO> goodMemberList = CuccOfferMemberCache.get(parentOfferInst.getOfferId(), ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        if (KiteListUtils.isEmpty(goodMemberList)) {
            return;
        }

        for (GoodsMemberDTO goodMember : goodMemberList) {
            String max = goodMember.getMax();
            String min = goodMember.getMin();
             // 最大值为0时，或者最大值和最小值不相等，这不处理
            if (KiteStringUtils.isEqual("0", max) || !KiteStringUtils.isEqual(max, min)) {
                continue;
            }
            List<MemberGoodDTO> memeberGoods = goodMember.getMemberGoods();
            if (KiteListUtils.isEmpty(memeberGoods)) {
                continue;
            }
            if (!KiteStringUtils.isEqual(max, String.valueOf(memeberGoods.size()))) {
                continue;
            }
            memeberGoods.stream().forEach(spec ->{
                Offer offer = OfferCache.get(spec.getGoodsSku());
                KiteOfferInst kiteOfferInst = operateSceneOfferInstService
                    .addOfferInst(sceneInstId, parentOfferInstId, offer.getOfferId(), null, custId, ContextUtil.getUserId().toString())
                .getResultObject();

                if (Objects.nonNull(kiteOfferInst)) {
                    CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "saleGoodsCode", parentOfferInst.getAttrValue("saleGoodsCode"));
                    CuccPosServiceUtils.setExtParamValue(kiteOfferInst, sceneInstId, "saleGoodsName", parentOfferInst.getAttrValue("saleGoodsName"));
                    operateSceneInstService.addInst(sceneInstId, kiteOfferInst);
                }
            });
        }
    }
}
